package com.bangcommunity.bbframe.dynconfig.id;

import com.bangcommunity.bbframe.common.utils.id.DangDangIdGenerator;
import org.apache.curator.framework.recipes.atomic.DistributedAtomicLong;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 *
 * @author tanghc
 * @date 17/1/23
 */
public class DistAppIdentifier {
    static final Logger logger = LoggerFactory.getLogger(DistAppIdentifier.class);
    private static ConcurrentMap<String, DistributedAtomicLong> seeds = new ConcurrentHashMap<>();

    private DistAppIdentifier() {
    }

    private static class DistAppIdentifierHolder {
        private static DistAppIdentifier instance = new DistAppIdentifier();
        private static final String dist_dang_id_key = "d_d_i_k";
        private static long initWorkId = ZkId.getInstance().nextId(dist_dang_id_key)
                % (DangDangIdGenerator.WORKER_ID_MAX_VALUE - 1);
    }

    public static DistAppIdentifier getInstance() {
        return DistAppIdentifierHolder.instance;
    }

    public int appIndentifier() {
        long initWorkId = DistAppIdentifierHolder.initWorkId;
        logger.warn("app work id {}", initWorkId);
        return new Long(initWorkId).intValue();
    }
}
